<template>
	<view class="ranking">
		<view class="bg">
			<view class="top">
				<view class="box">
					<view class="list" :class="activeIndex == index? 'active':''" v-for="(item,index) in list"
						:key="index" @click="getList(index,item.type)">{{item.text}}</view>
				</view>
			</view>

			<view class="book-list">
				<view class="title">
					——— <text>推薦</text> ———
				</view>
				<view class="book" v-for="(item,index) in book" :key="index" @click="goDetail(item.sourceUrl)">
					<view class="img-box">
						<image :src="item.iconUrl" mode="widthFix"></image>
					</view>
					<view class="content">
						<view class="book-title">
							{{index+1}}.{{item.title}}
						</view>
						<view class="author">
							{{item.author}}
						</view>
						<view class="label">
							<view class="label-list">
								{{item.category}}
							</view>
							<view class="label-list">
								{{item.showTotalCount}}
							</view>
						</view>
						<view class="text">
							<text v-html="item.description"></text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ranking',
		data() {
			return {
				activeIndex: 0,
				book: [],
				gender: '',
				list: [{
						text: '日榜',
						type: 'day'
					},
					{
						text: '周榜',
						type: 'week'
					},
					{
						text: '月榜',
						type: 'month'
					}
				]
			};
		},
		methods: {
			getList(index, type) {
				if (this.activeIndex == index) {
					return;
				}
				uni.showLoading({
					title: '正在加载中'
				});
				this.activeIndex = index;
				if (this.gender == "") {
					this.getNewBook(type)
				} else {
					this.getBook(this.gender, type);
				}

			},
			getBook(gender, type) {
				uni.request({
					url: `https://apis.netstart.cn/yunyuedu/rank/original/list/data.json?gender=${gender}&sortType=${type}&type=sell`,
					method: 'GET',
				}).then(res => {
					// console.log('res ==> ', res);
					this.book = res.data.data.books
					// console.log('this.book ==> ', this.book);
					uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			getNewBook(type) {
				uni.request({
					url: `https://apis.netstart.cn/yunyuedu/rank/publish/list/data.json?sortType=${type}&type=sell`,
					method: 'GET',
				}).then(res => {
					// console.log('res ==> ', res);
					this.book = res.data.data.books
					// console.log('this.book ==> ', this.book);
					uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			goDetail(id) {
				id = id.replace('/source/', '')
				let user = uni.getStorageSync('userInfo');
				if (!user) {
					uni.showToast({
						title: '未登录,请先去登录',
						icon: 'none'
					})
				} else {
					uni.navigateTo({
						url: '../detail/detail?id=' + id
					})
				}
			}
		},
		onLoad(option) {
			uni.showLoading({
				title: '正在加载中'
			});
			if (option.gender == "") {
				this.getNewBook(this.list[0].type);
			} else {
				this.gender = option.gender;
				this.getBook(option.gender, this.list[0].type);
			}

		},
	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		height: 100vh;
		background-image: url('../../static/background-image.png');
		background-repeat: no-repeat;
	}

	.top {
		width: 100%;
		height: 200rpx;
		display: flex;
		justify-content: center;
		align-items: flex-end;

		.box {
			width: 675rpx;
			height: 150rpx;
			background-color: #CEA880;
			border-top-left-radius: 20px;
			border-top-right-radius: 20px;
			display: flex;
			align-items: center;
			justify-content: space-evenly;

			.list {
				width: 144rpx;
				height: 62rpx;
				background-color: #FDECD0;
				text-align: center;
				line-height: 62rpx;
				color: #595959;
				font-size: 28rpx;
				border-radius: 62rpx;
				border: 1px solid #FC768A;
			}

			.active {
				color: white;
				background-color: #FE855A;
			}
		}


	}

	.book-list {
		width: 100%;
		background-color: #FCECCC;

		.title {
			text-align: center;
			padding-top: 20rpx;
			// padding-bottom: 20rpx;
			color: #A6A6A6;

			text {
				padding-left: 25rpx;
				padding-right: 25rpx;
				color: black;
			}

		}

		.book {
			width: 675rpx;
			height: 280rpx;
			margin: 40rpx auto;
			display: flex;

			.img-box {
				width: 200rpx;
				height: 280rpx;

				image {
					width: 100%;
					display: block;
					border-radius: 20rpx;
				}
			}

			.content {
				margin-left: 30rpx;

				.book-title {
					font-size: 36rpx;
					font-weight: 700;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
				}

				.author {
					font-size: 28rpx;
					color: #a8a8a8;
					margin-top: 20rpx;
				}

				.label {
					display: flex;
					margin-top: 20rpx;

					.label-list:nth-child(1) {
						margin-left: 0;
					}

					.label-list {
						font-size: 24rpx;
						color: #77828a;
						// width: 72rpx;
						padding-left: 10rpx;
						padding-right: 10rpx;
						height: 40rpx;
						background-color: #fce2b2;
						border: 1px solid #FC768A;
						border-radius: 10rpx;
						text-align: center;
						line-height: 40rpx;
						margin-left: 20rpx;
					}
				}

				.text {
					font-size: 30rpx;
					color: #77828a;
					width: 411rpx;
					height: 80rpx;
					margin-top: 20rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}

		}
	}
</style>